package com.xuecheng.user.factory;

import com.xuecheng.user.service.AuthService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 勾新杰
 * @version 1.0
 * @description:
 * @date 2025/4/12 12:53
 */
@Component
public class AuthServiceFactory implements InitializingBean {

    /**
     * 认证服务集合
     */
    private final List<AuthService> authServiceList;

    /**
     * 构造注入
     * @param authServiceList 认证服务列表
     */
    @Autowired
    public AuthServiceFactory(List<AuthService> authServiceList) {
        this.authServiceList = authServiceList;
    }

    /**
     * 认证服务集合
     */
    private final Map<String, AuthService> map = new HashMap<>();

    /**
     * 初始化
     * @throws Exception 初始化异常
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        authServiceList.forEach(authService -> {
            map.put(authService.getAuthType().getType(), authService);
        });
    }

    /**
     * 获取认证服务
     * @param authType 认证类型
     * @return 认证服务
     */
    public AuthService getService(String authType) {
        return map.get(authType);
    }
}
